<?php

namespace App\Http\Controllers\App;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Goods;
use App\Models\Banner;
use App\Models\User;
use App\Models\GoodsType;
use App\Models\Shop;
use App\Models\SystemBanner;
use Illuminate\Support\Facades\DB;

class IndexController extends Controller
{
    /**
     * banner 和 分类
     *
     * @return void
     */
    public function index()
    {
        $user_id = session('user_id');
        
        $info    = User::where('status', 1)->find($user_id);

        // 获取店铺id
        $shop_id = $info['shop_id'];

        $shop_info = Shop::where('status', Shop::STATUS_BEGIN)->select('id', 'shopname', 'avatar')->find($info['shop_id']);

        if ( $user_id == 0 || !$shop_info) {
      
            $shop_info = Shop::where('login', 80000)->select('id', 'shopname', 'avatar')->first();
        }
     
        // 获取店铺banner信息
        $banner = $this->banner($shop_info['id'] );

        // 获取分类信息
        $type   = $this->type();

        $return_data = [
            'shop'   => $shop_info,
            'banner' => $banner,
            'type'   => $type,
            'operator' => asset('/regis/index.html'),
        ];

        return self::json( $return_data, '查询成功');

    }

    /**
     * 获取banner图
     *
     * @return void
     */
    private function banner( $shop_id )
    {
        $banner = [];

        $data =   SystemBanner::where('position', 1)
                            ->select(DB::raw('id, img_url as url'))
                            ->where('status', 1)
                            ->where('isdelete', 0)
                            ->orderBy('display')
                            ->get()
                            ->toArray();

        if (empty($data)) {

            $banner[] = ['id' => '','url' => ''];
            return $banner;
        }

        return $data;
    }

    /**
     * 获取首页分类
     *
     * @return void
     */
    private function type()
    {
        $type_info =  GoodsType::where('pid', 0)
                                ->select('id', 'name', 'app_pic')
                                ->where('status', 1)
                                ->orderBy('sort')
                                ->skip(0)
                                ->take(7)
                                ->get();

        return $type_info ? $type_info : [];
    }

    /**
     * 展示商品
     *
     * @return void
     */
    public function goods( Request $request )
    {   
        $name = $request->input('name');
        
        $where[] = ['status', '=', 1];

        if ( $name ) {
            $where[] = ['name', 'like', '%' . $name . '%'];
        }

        $data = Goods::where($where)
                    ->select('id', 'pic', 'name', 'retail_price', 'market_price')
                    ->orderBy('sort')
                    ->orderBy('id', 'desc')
                    ->where('status', 1)
                    ->where('is_sell', 1)
                    ->paginate(10)->toArray();
        
        $data = Goods::return_goods($data);
         
        return self::json($data);
    }
}
